package com.yh.csx.crm.webapi.controller;

import com.yh.csx.crm.appapi.controller.BaseController;
import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.service.request.DevCustomerAddReq;
import com.yh.csx.crm.service.request.DevCustomerListReq;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.core.util.DateUtils;
import com.yh.csx.crm.po.Customer;
import com.yh.csx.crm.service.CustomerService;
import com.yh.csx.crm.service.DevCustomerService;
import com.yh.csx.crm.service.response.DevCustomerCheckResp;
import com.yh.csx.crm.service.response.DevCustomerListResp;
import com.yh.csx.crm.service.response.DevCustomerResp;
import com.yh.csx.crm.service.vo.PageWrap;
import com.yh.csx.crm.service.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * DevCustomerController class
 *
 * @author huyalin
 * @date 2019/4/10
 */
@Slf4j
@Api("待开发客户管理服务")
@RestController
@RequestMapping("/dev_customer")
public class DevCustomerController extends BaseController {

    @Value("${dev.customer.limit}")
    private int devCustomerLimit;

    @Autowired
    CustomerService customerService;

    @Autowired
    DevCustomerService devCustomerService;

    @ApiOperation("客户列表")
    @GetMapping("/list")
    public CommonResponse<PageWrap<List<DevCustomerListResp>>> getCustomerList(DevCustomerListReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            if (Objects.nonNull(req.getBeginDate()) && Objects.nonNull(req.getEndDate())) {
                req.setBeginDate(DateUtils.oneDayBeginTime(req.getBeginDate()));
                req.setEndDate(DateUtils.oneDayEndTime(req.getEndDate()));
            }
            return devCustomerService.getDevCustomerList(user, req);
        });
    }

    @ApiOperation("获取客户详情")
    @GetMapping("/get")
    public CommonResponse<DevCustomerResp> getCustomer(Long customerId) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            return devCustomerService.getCustomer(customerId);
        });
    }

    @ApiOperation("新增和编辑")
    @PostMapping("/add")
    public CommonResponse addOrSaveCustomer(@RequestBody @Validated DevCustomerAddReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            devCustomerService.addDevCustomer(user, req);
        });
    }

    @ApiOperation("校验")
    @PostMapping("/check")
    public CommonResponse<List<DevCustomerCheckResp>> checkCustomer(String customerName) {
        return super.visit(() -> {
            return devCustomerService.checkDevCustomer(customerName);
        });
    }

    @ApiOperation("捡回01和开放02")
    @PostMapping("/change")
    public CommonResponse change(Long customerId, String type) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            if (type.equals("01")) {
                if (devCustomerService.devCustomerLimit(user)) {
                    throw new BusinessException("捡回失败，已超过私海容量限制");
                }
                Customer customer = customerService.getCustomer(customerId);
                Optional.ofNullable(customer).orElseThrow(() ->  new BusinessException("客户不存在"));
                if (customer.getSalesUserId().compareTo(0L) != 0) {
                    throw new BusinessException("他人私海的客户不能捡回");
                }
                customerService.changeCustomer(user, customerId, user.getUserId());
            } else if (type.equals("02")) {
                customerService.changeCustomer(user, customerId, 0L);
            } else {
                throw new BusinessException("类型不正确");
            }
        });
    }

    @ApiOperation("信控申请")
    @PostMapping("/to_customer")
    public CommonResponse toCustomer(Long customerId) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            devCustomerService.toCustomer(user, customerId);
        });
    }
}
